package com.fand.common.support;

import java.util.List;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.fand.acceptControl.dto.NavigationDto;
import com.fand.common.service.CommonService;
import com.fand.configuration.dto.DictionaryDeDto;
import com.fand.configuration.dto.DictionaryDto;
import com.fand.configuration.dto.DictionaryTyDto;
import com.fand.configuration.dto.VariableDto;
import com.fand.site.dto.SiteDto;

public class InitDataSupport {

	private CommonService commonService;

	private DictionaryTyDto defaultDictionary;

	private SiteDto defaultSite;

	private List<VariableDto> defaultVariableList;

	private Boolean initData;

	public void init() {
		if (!BooleanUtils.isTrue(this.initData)) {
			return;
		}

		if (this.defaultDictionary != null) {
			this.initDictionaryTy(this.defaultDictionary);
		}

		if (this.defaultSite != null) {
			this.initSite(this.defaultSite);
		}

		if (!CollectionUtils.isEmpty(this.defaultVariableList)) {
			this.initVariable(this.defaultVariableList);
		}
	}

	/**
	 * 初始化：字典
	 * 
	 * @param dictionaryList
	 *            字典集合
	 * @param typeId
	 *            类别标识
	 */
	private void initDictionary(final List<DictionaryDto> dictionaryList, final String typeId) {
		if (CollectionUtils.isEmpty(dictionaryList)) {
			return;
		}

		for (final DictionaryDto dictionary : dictionaryList) {
			dictionary.setTypeId(typeId);

			final String id = this.commonService.getConfigurationService().getDictionaryService()
					.saveDictionaryBy1ForObject(dictionary);

			this.initDictionaryDe(dictionary.getDefaultCo(), id);
		}
	}

	/**
	 * 初始化：字典默认
	 * 
	 * @param defaultCo
	 *            默认编号
	 * @param entityId
	 *            实体标识
	 */
	private void initDictionaryDe(final String defaultCo, final String entityId) {
		final boolean isDefault = BooleanUtils.toBoolean(defaultCo);

		if (!isDefault) {
			this.commonService.getConfigurationService().getDictionaryService().deleteDictionaryDeBy1(entityId);

			return;
		}

		final DictionaryDeDto dictionaryDe = new DictionaryDeDto();

		dictionaryDe.setEntityId(entityId);

		this.commonService.getConfigurationService().getDictionaryService().saveDictionaryDeBy1(dictionaryDe);
	}

	/**
	 * 初始化：字典类型
	 * 
	 * @param dictionaryTy
	 *            字典类型对象
	 */
	private void initDictionaryTy(final DictionaryTyDto dictionaryTy) {
		final String id = this.commonService.getConfigurationService().getDictionaryService()
				.saveDictionaryTyBy1ForObject(dictionaryTy);

		this.initDictionary(dictionaryTy.getDictionaryList(), id);

		if (CollectionUtils.isEmpty(dictionaryTy.getChilds())) {
			return;
		}

		for (final DictionaryTyDto childDictionaryTy : dictionaryTy.getChilds()) {
			childDictionaryTy.setParentId(id);

			this.initDictionaryTy(childDictionaryTy);
		}
	}

	/**
	 * 初始化：导航
	 * 
	 * @param navigation
	 *            导航对象
	 */
	private void initNavigation(final NavigationDto navigation) {
		if (StringUtils.isBlank(navigation.getTypeId())) {
			final String typeId = this.commonService.getConfigurationService().getDictionaryService()
					.selectDictionaryBy1ForObject("NAVIGATION_TY", navigation.getTypeCo());

			navigation.setTypeId(typeId);
		}

		final String id = this.commonService.getAcceptControlService().getNavigationService().saveNavigationBy1ForObject(navigation);

		if (CollectionUtils.isEmpty(navigation.getChilds())) {
			return;
		}

		for (final NavigationDto childNavigation : navigation.getChilds()) {
			childNavigation.setSiteId(navigation.getSiteId());

			childNavigation.setStateId(navigation.getStateId());

			childNavigation.setTypeCo(navigation.getTypeCo());

			childNavigation.setTypeId(navigation.getTypeId());

			childNavigation.setParentId(id);

			this.initNavigation(childNavigation);
		}
	}

	/**
	 * 初始化：站
	 * 
	 * @param site
	 *            站对象
	 */
	private void initSite(final SiteDto site) {
		final String id = this.commonService.getSiteService().saveSiteBy1ForObject(site);

		if (!CollectionUtils.isEmpty(site.getNavigationList())) {
			final String stateId = this.commonService.getConfigurationService().getDictionaryService()
					.selectDictionaryBy1ForObject("NavigationState", "1");

			for (final NavigationDto navigation : site.getNavigationList()) {
				navigation.setSiteId(id);

				navigation.setStateId(stateId);

				this.initNavigation(navigation);
			}
		}
	}

	/**
	 * 初始化：变量
	 * 
	 * @param variableList
	 *            变量集合
	 * @param siteId
	 *            站标识
	 */
	private void initVariable(final List<VariableDto> variableList) {
		if (CollectionUtils.isEmpty(variableList)) {
			return;
		}

		for (final VariableDto variable : variableList) {
			final String typeId = this.commonService.getConfigurationService().getDictionaryService()
					.selectDictionaryBy1ForObject("VariableType", variable.getTypeCo());

			variable.setTypeId(typeId);

			this.commonService.getConfigurationService().getVariableService().saveVariableBy1ForObject(variable);
		}
	}

	public void setCommonService(final CommonService commonService) {
		this.commonService = commonService;
	}

	public void setDefaultDictionary(final DictionaryTyDto defaultDictionary) {
		this.defaultDictionary = defaultDictionary;
	}

	public void setDefaultSite(final SiteDto defaultSite) {
		this.defaultSite = defaultSite;
	}

	public void setDefaultVariableList(final List<VariableDto> defaultVariableList) {
		this.defaultVariableList = defaultVariableList;
	}

	public void setInitData(final Boolean initData) {
		this.initData = initData;
	}
}